﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Manager.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Import Namespace="B2B.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<h2>ProductsManagement</h2>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="HEAD" runat="server">
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="main_content" runat="server">
    <h6> Management -> Product</h6>
    <script type="text/javascript">
        function loadDanhMuc(maLinhVuc) {
            if (maLinhVuc != 0) {
                $.getJSON("/Home/DanhSachDanhMuc?id=" + maLinhVuc,
                 function (data) {
                     var s = '';
                     s += "<select id='MaDanhMuc' name='maDanhMuc'>";
                     for (i = 0 ; i < data.length; i++) {
                         s += "<option onclick='loadDanhSachSP(" + data[i].MaDanhMuc + ", 1)' value='" + data[i].MaDanhMuc + "'>" + data[i].TenDanhMuc + "</option>";
                     }
                     s += "</select>";
                     $("#divDanhMuc").html(s);
                 });
            }           
        }

        function loadDanhSachSP(page) {
                $.getJSON("/Manage/ProductList?page="+ page,
                    function (data) {
                        if (data.length > 0) {
                            var s = "";
                            s += "<table border='0'>";
                            s += "<tr style='background-color:#177f75; font-size:17px;color:#a6fdbf'>";
                            s += "<th style='padding:5px; width:300px ' >Name </th>";
                            s += "<th style='padding:5px; width:100px ' >Price </th>";
                            s += "<th style='padding:5px; width:100px ' >Min. Order </th>";
                            s += "<th style='padding:5px; width:100px ' >Payment Terms </th>";
                            s += "<th style='padding:5px; width:100px ' >status </th>";
                            s += "<th style='padding:5px; width:50px ' >Edit </th>";
                            s += "<th style='padding:5px; width:50px ' >Delete </th>";
                            s += "</tr>";
                            for (i = 0; i < data.length; i++) {
                                var loaiTinhTrang = "";
                                if(data[i].TinhTrang == 0)
                                    loaiTinhTrang = "Waiting";
                                if(data[i].TinhTrang == 1)
                                    loaiTinhTrang = "Rejected";
                                if(data[i].TinhTrang == 2)
                                    loaiTinhTrang = "Accepted";
                                s += "<tr>";
                                s += " <td style='padding:5px'><a href='/Product/ProductDetail/" + data[i].MaSanPham + "'>" + data[i].TenSanPham + "</a></td>";
                                s += " <td style='padding:5px'>" + data[i].Gia + "</td>";
                                s += " <td style='padding:5px'>" + data[i].SoLuongDatToiThieu + " " +data[i].TenDVT + "</td>";
                                s += " <td style='padding:5px'>" + data[i].LoaiThanhToan + "</td>";
                                s += " <td style='padding:5px'>" + loaiTinhTrang + "</td>";
                                s += " <td style='padding:5px'><a href='/Manage/UpdateProduct?maSP=" + data[i].MaSanPham + "' class='btn'>Edit</a></td>";
                                s += "  <td style='padding:5px'><input type='button' onclick=' XoaSanPham(" +data[i].MaSanPham+")' value='Delete' class='btn' /> </td>";
                                s += "</tr>";
                            }
                            s += "</table>";
                            $("#divDanhSachSP").html(s);
                        }
                        else
                            $("#divDanhSachSP").html("");
                    });
        }

        function XoaSanPham(maSP) {
            $.getJSON("/Manage/DeleteProduct?maSP=" + maSP,
                function (data) {
                    if (data.length > 0 & data != "fail") {
                        var s = "";
                        s += "<table border='0'>";
                        s += "<tr style='background-color:#177f75; font-size:17px;color:#a6fdbf'>";
                        s += "<th style='padding:5px; width:300px ' >Name </th>";
                        s += "<th style='padding:5px; width:100px ' >Price </th>";
                        s += "<th style='padding:5px; width:100px ' >Min. Order </th>";
                        s += "<th style='padding:5px; width:100px ' >Payment Terms </th>";
                        s += "<th style='padding:5px; width:100px ' >status </th>";
                        s += "<th style='padding:5px; width:50px ' >Edit </th>";
                        s += "<th style='padding:5px; width:50px ' >Delete </th>";
                        s += "</tr>";
                        for (i = 0; i < data.length; i++) {
                            var loaiTinhTrang = "";
                            if(data[i].TinhTrang == 0)
                                loaiTinhTrang = "Waiting";
                            if(data[i].TinhTrang == 1)
                                loaiTinhTrang = "Rejected";
                            if(data[i].TinhTrang == 2)
                                loaiTinhTrang = "Accepted";
                            s += "<tr>";
                            s += " <td style='padding:5px'><a href='/Product/ProductDetail/" + data[i].MaSanPham + "'>" + data[i].TenSanPham + "</a></td>";
                            s += " <td style='padding:5px'>" + data[i].Gia + "</td>";
                            s += " <td style='padding:5px'>" + data[i].SoLuongDatToiThieu + " " + data[i].TenDVT + "</td>";
                            s += " <td style='padding:5px'>" + data[i].LoaiThanhToan + "</td>";
                            s += " <td style='padding:5px'>" + loaiTinhTrang + "</td>";
                            s += " <td style='padding:5px'><a href='/Manage/UpdateProduct?maSP="+ data[i].MaSanPham +"' class='btn'>Edit</a></td>";
                            s += "  <td style='padding:5px'><input type='button' onclick=' XoaSanPham(" +data[i].MaSanPham+")' value='Delete' class='btn' /> </td>";
                            s += "</tr>";
                        }
                        s += "</table>";
                        $("#divDanhSachSP").html(s);
                        alert("Xoa thanh cong");
                    }
                    else
                        alert("you can't delete this product");
                });
        }
    </script>  
    <a href="/Manage/CreateProduct">Create Product</a>  
    <div id="divDanhSachSP">
        <table border="0">
            <tr style='background-color:#177f75; font-size:17px;color:#a6fdbf'>
                <th style="padding:5px; width:300px" >Name</th>
                <th style="padding:5px; width:100px">Price</th>
                <th style="padding:5px; width:100px">Min. Order</th>
                <th style="padding:5px; width:100px">Payment Terms</th>
                <th style="padding:5px; width:100px">status</th>
                <th style="padding:5px; width:50px">Edit</th>
                <th style="padding:5px; width:50px">Delete</th>
            </tr>
            <%
                ArrayList list_SP = (ArrayList)ViewData["listSP"];
                foreach (var sp in list_SP)
                {
                    string maSP = ((SP_DanhSachSanPham)sp).MaSanPham.ToString();
                    string tenSP = ((SP_DanhSachSanPham)sp).TenSanPham;
                    string gia = ((SP_DanhSachSanPham)sp).Gia.ToString();
                    string soluongdattoithieu = ((SP_DanhSachSanPham)sp).SoLuongDatToiThieu.ToString();
                    string loaithanhtoan = ((SP_DanhSachSanPham)sp).LoaiThanhToan;
                    int tinhtrang = ((SP_DanhSachSanPham)sp).TinhTrang;
                    string tenDVT = ((SP_DanhSachSanPham)sp).TenDVT;
                    string loaiTinhTrang = "Waitting";
                    if (tinhtrang == 1)
                        loaiTinhTrang = "Rejected";
                    if(tinhtrang == 2)
                        loaiTinhTrang = "Accepted";
            %>
                <tr>
                    <td style="padding:5px"><a href="/Product/ProductDetail/<%=maSP %>"><%=tenSP %></a></td>
                    <td style="padding:5px"><%=gia %> VND</td>
                    <td style="padding:5px"><%=soluongdattoithieu %> <%=tenDVT %></td>
                    <td style="padding:5px"><%=loaithanhtoan %></td>
                    <td style="padding:5px"><%=loaiTinhTrang %></td>
                    <td style="padding:5px"><a class="btn" href="#" onclick="alert('Function is building')">Edit</a></td>
                    <td style="padding:5px">
                        <input type="button" onclick=" XoaSanPham(<%=maSP %>)" value="Delete" class="btn" />

                    </td>
                </tr>
            <%} %>
          
              
        </table>
       
    </div>
     <div>
      
         
         
           <div id="trigger-list">
      <%
          int tongpage =  int.Parse(ViewData["tongPage"].ToString());
          
          for(int i = 1; i <= tongpage; i++)
          {
      %>
          <input type="button"class="trigger error-trigger" value="<%=i %>" onclick ="loadDanhSachSP(<%=i %>)"/> 
      <%} %>
      </div>
           
     </div>
</asp:Content>
